/* Bootstrap_Mismeasurement.do

Note: Requires "ssc inst _gwtmean" for some code. Also, the thing is riddled 
with errors and often doesn't run succesfully. Beats me why.

USING Define_Extensions.do as instructions.

I will create bootstrapped standard errors for all the columns in the formatted
Aguiar and bils replication tables... these errors can be found in the excel 
folder!

One repetition of the bootstrap takes about 100 seconds, comes out to about
2.75 hours for 100 repetitions

Note that the current definition od second_stage_all calculates the DIFFERENCES
in log inequality for all exccept the first period of time. The other 
definitions calculate levels. (The difference cones from the option "noomit" in
the construction of DdecX vars)

For Mismeasurement:
"Phi_i_t" from the paper is reported as "income-specific measurement error. 
In the code, this ends up being the coefficients on DdecX vars. Specificially,
the paper reports Phi_5_t - Phi_1_t, which is Ddec_5_t (Error for high income
group relative to low income group.
I also report Phi_3_t, and Phi_5_t-Phi_3_t.
*/

clear all
set more off
ssc inst _gwtmean
capture ssc inst estout
mata mata mlib index

set matsize 10000
//set matsize 800
cd "$path_name"

version 9

qui do Define_Extensions.do
//qui do benchmark_define_crc.do
//second_stage_benchmark
// This also loads up the proper data file.

timer clear
// Switch this on to run the column thing...
second_stage_dataset
// Define globals for columns of import:

global v6 sheltr fdhomer vehpchr oppubr utilr healthr equpmtr fdawayr tvrdotr adultclr feeardr cashcor furnshr



// MISMEASUREMENT --------------------------------------------------------------
	global blist_mismes
	foreach spec in OLS{
		foreach num of numlist 1/9 {
			foreach num2 of numlist 3 5 {
				global blist_mismes $blist_mismes  `spec'b_`num'_`num2' = r(`spec'b_`num'_`num2')
				global blist_mismes $blist_mismes OLSD_`num'_`num2' = r(OLSD_`num'_`num2')
			}
		}
	}
	foreach num in 3 5{
		foreach p in 4to6 6to8 8to9{
			global blist_mismes $blist_mismes IneqChange_`num'_`p' = r(IneqChange_`num'_`p')
			global blist_mismes $blist_mismes MismesChange_`num'_`p' = r(MismesChange_`num'_`p')
		}
	}	

	global blist_mismes $blist_mismes PanB1 = r(PanB1) PanB4 = r(PanB4) ///
	PanB4to6 = r(PanB4to6) PanB6to8 = r(PanB6to8) PanB8to9 = r(PanB8to9) ///
	PanB1to9 = r(PanB1to9) ///
	PanB1_mismes = r(PanB1_mismes) PanB4_mismes = r(PanB4_mismes) ///
	PanB4to6_mismes = r(PanB4to6_mismes) PanB6to8_mismes = r(PanB6to8_mismes) ///
	PanB8to9_mismes = r(PanB8to9_mismes) PanB1to9_mismes = r(PanB1to9_mismes)	
	
bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all
estout using "Excel/bs_mismes_col1.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr equpmtr tvrdotr
estout using "Excel/bs_mismes_col2.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr equpmtr fdawayr tvrdotr 
estout using "Excel/bs_mismes_col3.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr healthr oppubr utilr equpmtr fdawayr tvrdotr adultclr
estout using "Excel/bs_mismes_col4.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr healthr equpmtr fdawayr tvrdotr adultclr ///
feeardr cashcor
estout using "Excel/bs_mismes_col5.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all $v6
estout using "Excel/bs_mismes_col6.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all $v6 housopr
estout using "Excel/bs_mismes_col7.xls", cells(b se) stats(N_clust N_reps) replace

bs $blist_mismes, reps(100) seed(1) cluster(id): second_stage_all $v6 housopr educar
estout using "Excel/bs_mismes_col8.xls", cells(b se) stats(N_clust N_reps) replace
